package com.canbot.u05.activity.eat;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.EatMsgInfo;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.StatusBarUtil;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;
import com.google.gson.Gson;

import java.util.HashMap;

/**
 * Created by zmp on 2017/11/20.
 * 充电设置主界面
 */

public class EatActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private Button backBt;

        private Button openBt;

        private Button markBt;

        private ToastTools toastTools;

        private TextView nameTv;

        private Dialog hintDialog;

        private TextView notTv;

        private View canLL;

        private Dialog dialogLoading;

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_layout_eat);
                initView();
                initData();
        }

        private void initData() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                getMapInfo();
        }


        @Override
        protected void onResume() {
                super.onResume();
        }

        private void initView() {
                toastTools = ToastTools.newInstance(this);
                nameTv = (TextView) findViewById(R.id.eat_name_tv);
                TextView title = (TextView) findViewById(R.id.guide_base_title_tv);
                title.setText("自动充电");

                notTv = (TextView) findViewById(R.id.eat_not_tv);
                canLL = findViewById(R.id.eat_yes_ll);

                backBt = (Button) findViewById(R.id.eat_bt_back);
                openBt = (Button) findViewById(R.id.eat_bt_open);
                markBt = (Button) findViewById(R.id.eat_mark);
                backBt.setOnClickListener(this);
                openBt.setOnClickListener(this);
                markBt.setOnClickListener(this);
        }

        private EatMsgInfo eatMsgInfo;

        private void getMapInfo() {
                String url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 13 + "");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {


                        @Override
                        public void onSuccess(String result) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                Log.e("TTS2Voice", result);
                                if (TextUtils.isEmpty(result)) {
                                        startOtherActivity(EatMapListActivity.class, true);
                                        return;
                                }
                                eatMsgInfo = new Gson().fromJson(result, EatMsgInfo.class);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                handlerResult();
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.network_error));
                                finish();
                        }
                });
        }

        private void handlerResult() {
                if (eatMsgInfo == null) {
                        return;
                }
                boolean canEat = eatMsgInfo.isCanEat();
                openBt.setSelected(canEat);
                canLL.setVisibility(canEat ? View.VISIBLE : View.GONE);
                notTv.setVisibility(canEat ? View.GONE : View.VISIBLE);
                if (canEat) {
                        String mapName = eatMsgInfo.getMapName();
                        if (TextUtils.isEmpty(mapName)) {
                                startOtherActivity(EatMapListActivity.class, true);
                                return;
                        }
                        nameTv.setText("当前地图:" + mapName);
                        showHintDialog(mapName);
                        markBt.setSelected(eatMsgInfo.isMark());
                }
        }


        /**
         * 提示Dialog
         */
        private void showHintDialog(String currentMapName) {
                if (hintDialog == null) {
                        hintDialog = new Dialog(this, R.style.MyDialogStyle);
                        hintDialog.setContentView(R.layout.guide_dialog_hint);
                        hintDialog
                                .findViewById(R.id.guide_dialog_hint_sure)
                                .setOnClickListener(this);
                        hintDialog
                                .findViewById(R.id.guide_dialog_hint_move)
                                .setOnClickListener(this);

                }
                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(10));
                String s = "你当前加载的地图是\"" + currentMapName + "\",请确认机器处于对应的环境下。若环境已发生改变请前往切换地图!";
                TextView textView = (TextView) hintDialog.findViewById(R.id.guide_dialog_tv);
                textView.setText(s);
                StatusBarUtil.hideVirtualKey(hintDialog);
                hintDialog.show();
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.guide_dialog_hint_sure:
                                hintDialog.dismiss();
                                break;
                        case R.id.guide_dialog_hint_move:
                                startOtherActivity(EatMapListActivity.class, true);
                                hintDialog.dismiss();
                                break;
                        case R.id.eat_bt_back:
                                finish();
                                break;
                        case R.id.eat_bt_open:
                                boolean selected = v.isSelected();
                                v.setSelected(!selected);
                                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                                setCanEat(!selected);
                                break;
                        case R.id.eat_mark:
                                startOtherActivity(EatMarkHintOneActivity.class, true);
                                break;
                        default:
                }
        }

        public void setCanEat(final boolean canEat) {
                String url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 14 + "");
                hashMap.put("data", canEat + "");
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {


                        @Override
                        public void onSuccess(String result) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                Log.e("TTS2Voice", result);
                                eatMsgInfo.setCanEat(canEat);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                handlerResult();
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.network_error));


                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                openBt.setSelected(!openBt.isSelected());
                                        }
                                });

                        }
                });
        }

}
